<template>
	<view class="invoice-form-page">
		<view class="form-content">
			<view class="form-item">
				<view class="label">发票类型</view>
				<view class="input-wrap">
					<view 
						class="type-item" 
						:class="{'active': form.invoiceType === 1}"
						@click="form.invoiceType = 1"
					>个人</view>
					<view 
						class="type-item" 
						:class="{'active': form.invoiceType === 2}"
						@click="form.invoiceType = 2"
					>企业</view>
				</view>
			</view>
			
			<view class="form-item">
				<view class="label">发票抬头</view>
				<input 
					class="input" 
					type="text" 
					v-model="form.title"
					placeholder="请输入发票抬头"
				/>
			</view>
			
			<view class="form-item" v-if="form.invoiceType === 2">
				<view class="label">纳税人识别号</view>
				<input 
					class="input" 
					type="text" 
					v-model="form.taxNumber"
					placeholder="请输入纳税人识别号"
				/>
			</view>
			
			<view class="form-item">
				<view class="label">联系电话</view>
				<input 
					class="input" 
					type="number" 
					v-model="form.phone"
					maxlength="11"
					placeholder="请输入联系电话"
				/>
			</view>
			
			<view class="form-item">
				<view class="label">联系邮箱</view>
				<input 
					class="input" 
					type="text" 
					v-model="form.email"
					placeholder="请输入联系邮箱"
				/>
			</view>
			
			<view class="form-item">
				<view class="label">订单金额</view>
				<view class="amount">￥{{orderInfo.transAmount.toFixed(2)}}</view>
			</view>
		</view>
		
		<view class="submit-btn" @click="submitForm">提交申请</view>
	</view>
</template>

<script>
	import { submitInvoiceInfo } from '@/api/invoice'
	
	export default {
		data() {
			return {
				orderInfo: {},
				form: {
					invoiceType: 1, // 1-个人，2-企业
					title: '', // 发票抬头
					taxNumber: '', // 纳税人识别号
					phone: '', // 联系电话
					email: '', // 联系邮箱
				}
			}
		},
		onLoad(options) {
			if (options.orderInfo) {
				this.orderInfo = JSON.parse(decodeURIComponent(options.orderInfo))
			}
		},
		methods: {
			// 表单验证
			validateForm() {
				if (!this.form.title) {
					uni.showToast({
						title: '请输入发票抬头',
						icon: 'none'
					})
					return false
				}
				
				if (this.form.invoiceType === 2 && !this.form.taxNumber) {
					uni.showToast({
						title: '请输入纳税人识别号',
						icon: 'none'
					})
					return false
				}
				
				if (!this.form.phone) {
					uni.showToast({
						title: '请输入联系电话',
						icon: 'none'
					})
					return false
				}
				
				if (!/^1\d{10}$/.test(this.form.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return false
				}
				
				if (!this.form.email) {
					uni.showToast({
						title: '请输入联系邮箱',
						icon: 'none'
					})
					return false
				}
				
				if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this.form.email)) {
					uni.showToast({
						title: '请输入正确的邮箱',
						icon: 'none'
					})
					return false
				}
				
				return true
			},
			
			// 提交表单
			async submitForm() {
				if (!this.validateForm()) return
				
				try {
					uni.showLoading({
						title: '提交中...',
						mask: true
					})
					
					await submitInvoiceInfo({
						tripIds: this.orderInfo.orderId,
						invoiceTitle: this.form.title,
						taxNumber: this.form.taxNumber || undefined,
						invoiceType: this.form.invoiceType,
						contactPhone: this.form.phone,
						contactEmail: this.form.email
					})
					
					uni.hideLoading()
					uni.showToast({
						title: '提交成功，请等待处理',
						icon: 'none',
						duration: 2000
					})
					
					// 延迟跳转，让用户看到成功提示
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/invoice/invoice_list'
						})
					}, 1500)
					
				} catch (e) {
					uni.hideLoading()
					uni.showToast({
						title: e.message || '提交失败，请重试',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.invoice-form-page {
		min-height: 100vh;
		background-color: #f8f9fa;
		padding: 24rpx;
		padding-bottom: 140rpx;
		
		.form-content {
			background-color: #fff;
			padding: 40rpx 30rpx;
			margin-top: 20rpx;
			border-radius: 16rpx;
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
			
			.form-item {
				margin-bottom: 40rpx;
				
				.label {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 20rpx;
					font-weight: 500;
				}
				
				.input-wrap {
					display: flex;
					gap: 24rpx;
					
					.type-item {
						flex: 1;
						height: 88rpx;
						line-height: 88rpx;
						text-align: center;
						background-color: #f5f7fa;
						border-radius: 12rpx;
						font-size: 28rpx;
						color: #666;
						transition: all 0.3s ease;
						
						&.active {
							background-color: #2b65eb;
							color: #fff;
							box-shadow: 0 4rpx 12rpx rgba(43, 101, 235, 0.2);
						}
						
						&:active {
							transform: scale(0.98);
						}
					}
				}
				
				.input {
					height: 88rpx;
					background-color: #f5f7fa;
					border-radius: 12rpx;
					padding: 0 30rpx;
					font-size: 28rpx;
					transition: all 0.3s ease;
					
					&:focus {
						background-color: #fff;
						box-shadow: 0 0 0 2rpx rgba(43, 101, 235, 0.2);
					}
				}
				
				.amount {
					font-size: 40rpx;
					color: #ff4d4f;
					font-weight: bold;
					padding: 20rpx 30rpx;
					background-color: #fff1f0;
					border-radius: 12rpx;
					display: inline-block;
				}
			}
		}
		
		.submit-btn {
			position: fixed;
			bottom: 40rpx;
			left: 24rpx;
			right: 24rpx;
			height: 96rpx;
			line-height: 96rpx;
			background: linear-gradient(135deg, #3175ff, #2b65eb);
			color: #fff;
			font-size: 32rpx;
			text-align: center;
			border-radius: 48rpx;
			font-weight: 500;
			box-shadow: 0 8rpx 16rpx rgba(43, 101, 235, 0.2);
			transition: all 0.3s ease;
			
			&:active {
				transform: scale(0.98);
				box-shadow: 0 4rpx 8rpx rgba(43, 101, 235, 0.2);
			}
		}
	}
</style> 